package com.smap;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.smap.control.HistoryControl;
import com.smap.control.NavigateControl;
import com.smap.control.StatusControl;
import com.smap.control.VectorControl;
import com.smap.core.Map;
import com.smap.core.Marker;
import com.smap.event.MapEvent;
import com.smap.event.MapHandler;
import com.smap.layer.GoogleLayer;
import com.smap.layer.VectorLayer;
import com.smap.layer.WmsLayer;
import com.smap.vector.Feature;
import com.smap.vector.LineString;
import com.smap.vector.Point;
import com.smap.vector.Polygon;

public class Showcase implements EntryPoint, ClickHandler {
	Logger logger = Logger.getLogger("smapshow");
	Map map;

	public void onClick(ClickEvent event) {

	}

	public void onModuleLoad() {
		this.map = createMap();
		map.zoomByLonLat(144.96, -37.81, 12);
		RootLayoutPanel.get().add((Widget) map);
		logger.setLevel(Level.INFO);
		addBaseLayer();
		// addWMSLayer();
		addVectorLayer();
		//addMarkers();
	}

	private Map createMap() {
		map = new SMap();

		map.addControl(new NavigateControl());
		map.addControl(new StatusControl());
		map.addControl(new HistoryControl());

		map.addHandler(new MapHandler() {
			public void onMapEvent(MapEvent mapEvent) {
				logger.log(Level.FINE, mapEvent.toString());
			}
		}, MapEvent.TYPE);

		return map;
	}

	private void addMarkers() {
		String html = "<img src='flag.png' /><br/>hello!<br/><button>go</button>";
		map.addMarker(new Marker(144.96, -37.81, html, false).project(map
				.getProj()));
		map.addMarker(new Marker(144.96, -37.85, html, true).project(map
				.getProj()));
	}

	private void addBaseLayer() {
		GoogleLayer baseLayer = new GoogleLayer();
		map.addLayer(baseLayer);
	}

	private void addVectorLayer() {
		VectorLayer vLayer = new VectorLayer();
		map.addLayer(vLayer);
		{
			// String wkt =
			// "POLYGON ((130 110, 110 120, 120 140, 140 140, 130 110))";
			String wkt2 = "POLYGON ((145.0 -37.81, 144.95 -37.8, 144.90 -37.77, 145.0 -37.81))";
			{
				Feature feat = new Feature();
				feat.geom = new Point().add(145.0, -37.81).pack();
				feat.project(map.getProj());
				feat.label("I am a Point");
				vLayer.add(feat);
			}
			{
				Feature feat = new Feature();
				feat.geom = new LineString().add(145.0, -37.81)
						.add(145.01, -37.80).add(145.05, -37.83).pack();
				feat.style.fill_color = "#FFFFFF";
				feat.style.stroke_color = "#101010";
				feat.project(map.getProj());
				feat.label("I am a Line");
				vLayer.add(feat);
			}

			{
				Feature feat = new Feature();
				feat.geom = new Polygon().add(145.10, -37.91)
						.add(145.11, -37.85).add(145.05, -37.83).pack();
				feat.project(map.getProj());
				feat.label("I am a mighty Polygon");
				vLayer.add(feat);
			}
		}

		// set up control
		VectorControl vControl = new VectorControl(vLayer);
		vControl.activate();
		map.addControl(vControl);

	}

	private void addWMSLayer() {
		String server = "http://localhost/geoserver/shen/wms";
		String layer = "shen:Lga";
		WmsLayer wms = new WmsLayer(server, layer);
		// map.addLayer(wms);

		server = "http://reg.bom.gov.au/cgi-bin/reg/ws/gis/users/bomg0022/wxs";
		layer = "IDY0405202";
		WmsLayer wms2 = new WmsLayer(server, layer);
		// wms2.singleTile = true;
		map.addLayer(wms2);
	}
}
